package com.yanqu.road.server.manager.union;

import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manager.union.listener.CrossUnionHonorsListener;
import com.yanqu.road.server.manager.union.listener.CrossUnionHuaBenListener;
import com.yanqu.road.server.manager.union.listener.CrossUnionTradeListener;
import com.yanqu.road.server.manger.ICrossUnionSyncListener;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionNotifyMgr extends TempMgr {

    private static Logger logger = LogManager.getLogger(CrossUnionNotifyMgr.class.getName());

    static Map<Integer, Collection<ICrossUnionSyncListener>> listenerMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        reload();
        addListener(new CrossUnionTradeListener());
        addListener(new CrossUnionHuaBenListener());
        addListener(new CrossUnionHonorsListener());
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void addListener(ICrossUnionSyncListener listener){
        for (eUnionSyncType value : eUnionSyncType.values()) {
            addListener(value.getValue(), listener);
        }
    }

    public static void addListener(int type, ICrossUnionSyncListener listener) {
        Collection<ICrossUnionSyncListener> listListeners = listenerMap.get(type);
        if (listListeners == null) {
            // 创建线程安全版集合
            listListeners = Collections.synchronizedSet(new HashSet<>());
            listenerMap.put(type, listListeners);
        }

        if (!listListeners.contains(listener)) {
            listListeners.add(listener);
        }
    }

    public static void removeListener(int type, ICrossUnionSyncListener listener) {
        Collection<ICrossUnionSyncListener> listListeners = listenerMap.get(type);
        if (listListeners != null && listListeners.contains(listener)) {
            listListeners.remove(listener);
        }
    }

    public static boolean notifyListener(int type, long playerId, CrossUnionInfo unionInfo, List<CrossUnionMember> memberList) {

        List<ICrossUnionSyncListener> iSyncListeners = listenerMap.get(type) == null ? null
                : new ArrayList<>(listenerMap.get(type));
        if (iSyncListeners != null) {
            for (ICrossUnionSyncListener iSyncListener : iSyncListeners) {
                try {
                    iSyncListener.notifySync(type, playerId, unionInfo, memberList);
                } catch (Exception ex) {
                    logger.error("The class :" + iSyncListener.getClass(), ex);
                }
            }
            return true;
        }
        return false;
    }
}
